package com.ydd.service.impl;

import com.ydd.constant.JwtClaimsConstant;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.YddUserLoginDTO;
import com.ydd.entity.YddUser;
import com.ydd.mapper.YddUserMapper;
import com.ydd.properties.JwtProperties;
import com.ydd.result.Result;
import com.ydd.service.FrontLoginservice;
import com.ydd.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;


@Service
public class FrontLoginserviceImpl implements FrontLoginservice {
   /* @Autowired
    private JwtProperties jwtProperties;*/
    @Autowired
    private YddUserMapper yddUserMapper;

    @Override
    public Map<String, String> login(YddUserLoginDTO user) {
        //加密
        String psw = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        //账号为空 密码为空
        if (user.getMobile() == null || psw == null) {
            Result.error(MessageConstant.PASSWORD_ERROR);
        }

        //处理登录逻辑
        YddUser users = yddUserMapper.login(user.getMobile(), psw);
        if (users == null) {
            Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //登录成功返回token
        //加载载荷
       /* Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.YDD_USER_ID, users.getId());
        //生成令牌
        String token = JwtUtil.createJWT(
                jwtProperties.getFrontSecretKey()
                , jwtProperties.getBackTtl(),
                claims);
        Map<String, String> tokenmap = new HashMap<>();
        tokenmap.put("token", token);*/

        String token = JwtUtil.getToken(users.getId().toString());
        Map<String, String> tokenmap = new HashMap<>();
        tokenmap.put("token", token);
        return tokenmap;
    }
}